﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OnLineExamModel;
using System.Collections.Generic;
using OnLineExamDAL;
using System.Data.SqlClient;
using OnLineExamBLL;

public partial class Web_SingleSelectUpdate : System.Web.UI.Page
{
    SingleSelectedManager bll = new SingleSelectedManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Title = "修改试题";
        //展示绑定的数据并将它展示在下拉列表中

        if (!IsPostBack)
        {

            string userId = User.Identity.Name;
            var loginuser = new UserManager().GetModel(userId);
            string userName = loginuser.UserName;
            Label i1 = (Label)Page.Master.FindControl("labUser");
            i1.Text = userName;

            string depid = loginuser.DepID + "";
            #region  绑定部门 科目下拉菜单
            this.ddlCourse.DataTextField = "CourseName";
            this.ddlCourse.DataValueField = "CourseID";
            this.ddlCourse.DataSource = new OnLineExamBLL.Course().GetList("depid=" + depid);
            this.ddlCourse.DataBind();
            this.ddlCourse.Items.Insert(0, new ListItem("请选择科目", ""));


            this.ddlDepinfo.DataTextField = "Name";
            this.ddlDepinfo.DataValueField = "ID";
            this.ddlDepinfo.DataSource = new OnLineExamBLL.DepInfo().GetAllList();
            this.ddlDepinfo.DataBind();
            this.ddlDepinfo.Items.Insert(0, new ListItem("请选择", ""));

            this.ddlDepinfo.Items.FindByValue(depid).Selected = true;

            this.ddlDepinfo.Enabled = false;

            if (User.IsInRole("管理员"))
            {
                this.ddlDepinfo.Enabled = true;
            }
            #endregion


            //根据ID展示相应的值
            int id=int.Parse(Request["ID"].ToString());
            var model = bll.GetModel(id);
            txtTitle.Text = model.Title;
            txtAnswerA.Text =model.AnswerA;
            txtAnswerB.Text =model.AnswerB;
            txtAnswerC.Text = model.AnswerC;
            txtAnswerD.Text = model.AnswerD;
            ddlAnswer.SelectedValue = model.Answer;
            this.ddlCourse.Items.FindByValue(model.CourseID+"").Selected = true;//默认选定科目
           
        }
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        string depid = "";
        if (Session["depid"] != null)
        {
            depid = Session["depid"].ToString();
            // this.ddlCourse.Items.FindByValue(courseid).Selected = true;
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
        SingleProblem singleProble = new SingleProblem();
        singleProble.ID = int.Parse(Request["ID"].ToString());
        singleProble.DepID = int.Parse(depid);
        singleProble.CourseID = Convert.ToInt32(ddlCourse.SelectedValue);
        singleProble.Title = txtTitle.Text;
        singleProble.AnswerA = txtAnswerA.Text;
        singleProble.AnswerB = txtAnswerB.Text;
        singleProble.AnswerC = txtAnswerC.Text;
        singleProble.AnswerD = txtAnswerD.Text;
        singleProble.Answer = ddlAnswer.SelectedValue;
        if (bll.Update (singleProble) )
        {
            lblMessage.Text = "修改成功!";
            txtTitle.Text = string.Empty;
            txtAnswerA.Text = string.Empty;
            txtAnswerB.Text = string.Empty;
            txtAnswerC.Text = string.Empty;
            txtAnswerD.Text = string.Empty;
        }
        else
        {
            lblMessage.Text = "修改失败!";
        }

    }
    protected void imgBtnReturn_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("SingleSelectManage.aspx");
    }
}
